package com.zmj.controller;

import com.alibaba.fastjson.JSON;
import com.zmj.entity.UserInfo;
import com.zmj.service.IUserInfoService;
import com.zmj.service.impl.UserInfoServiceImpl;
import com.zmj.utils.Message;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/setInfo")
public class SetInfoServlet extends HttpServlet {
    IUserInfoService userInfoService = new UserInfoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String m = req.getParameter("uid");

        String sex = req.getParameter("sex");
        String rname = req.getParameter("rname");
        String phone = req.getParameter("phone");
        UserInfo userInfo = new UserInfo();
        Message message=new Message();
        if (m==null||"".equals(m.trim())){
            userInfo.setSex(sex);
            userInfo.setPhone(phone);
            userInfo.setRname(rname);
           boolean flg= userInfoService.addInfo(userInfo);
           if (flg){
               message.setMes("add success");
               message.setCode(1);
           }else {
               message.setCode(0);
               message.setMes("add fail");
           }
          resp.getWriter().print(JSON.toJSONString(message));
        }else {
            long uid=Long.parseLong(m);
            userInfo.setUid(uid);
            userInfo.setSex(sex);
            userInfo.setPhone(phone);
            userInfo.setRname(rname);
            boolean flg = userInfoService.setInfo(userInfo);

            if (flg) {
                message.setCode(1);
                message.setMes("update success");

            } else {
                message.setCode(0);
                message.setMes("update fail");
            }
            resp.getWriter().print(JSON.toJSONString(message));
        }

    }
}
